package service;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import common.kit.PageSql;
import model.ComsumablesRepalce;
import model.Staff;
import model.Station;

public class ComsumablesRepalceService extends BaseService {

	public Page<Record> getPage(Kv kv , Integer pageNumber , Integer pageSize) {
		String select = " tcr.id,tcr.createTime,tcr.deviceName,tcr.comsumablesName,tcr.count,tcr.unit,tcr.remark,ts.name,tc.name AS companyName1,tsr.name AS servicerName,tcr.recorder " ;
	    PageSql pageSql = PageSql.bySelect( kv , select ) ;
	    pageSql.from(" t_comsumables_repalce tcr ") ;
	    pageSql.join(" t_station ts " , " ts.id = tcr.stationId ") ;
	    pageSql.join(" t_servicer tsr " , " tsr.id = tcr.serviserId ") ;
	    pageSql.join(" t_company tc " , " tc.id = ts.companyId ") ;
		pageSql.setNotLessThanCondition("tcr.createTime","tcr.createTime.start") ; //创建时间
		pageSql.setNotMoreThanCondition("tcr.createTime","tcr.createTime.end") ; //创建时间
		pageSql.setEqualCondition("tcr.stationId") ; //点位	 
		pageSql.setLikeCondition("ts.name") ; //点位名称	 
		pageSql.setLikeCondition("tsr.name") ; //运维公司	 
		pageSql.setLikeCondition("tc.name") ; //企业名称	 
	    pageSql.orderBy(" tcr.createTime DESC ");
		return pageSql.paginate( pageNumber , pageSize) ;
	}

	public Page<Record> getApiPage(Kv kv , Integer pageNumber , Integer pageSize) {
		String select = " tcr.id,tcr.createTime,tcr.recorder,tcr.deviceName,tcr.comsumablesName,tcr.count,tcr.unit,tcr.remark,ts.name " ;
	    PageSql pageSql = PageSql.bySelect( kv , select ) ;
	    pageSql.from(" t_comsumables_repalce tcr ") ;
	    pageSql.join(" t_servicer ts " , " ts.id = tcr.serviserId ") ;
		pageSql.setEqualCondition("tcr.stationId") ; //点位	 
		pageSql.setNotLessThanCondition("tcr.createTime", "startTime");
		pageSql.setNotMoreThanCondition("tcr.createTime", "endTime");
	    pageSql.orderBy(" tcr.createTime DESC ");
		return pageSql.paginate( pageNumber , pageSize) ;
	}

	public Ret save(ComsumablesRepalce comsumablesRepalce , Staff staff , Station station) {
		comsumablesRepalce.setStationId(station.getId());
		comsumablesRepalce.setServiserId(staff.getServicerId());
		comsumablesRepalce.setRecorder(staff.getName());
		comsumablesRepalce.save();
	    return ok() ;
	}
}
